package io.renren.modules.score.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.img.entity.ProveEntity;
import io.renren.modules.img.service.ProveService;
import io.renren.modules.score.entity.ScoreEntity;
import io.renren.modules.score.service.ScoreService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;


/**
 *
 *
 * @author LMY
 * @email 2269467209@qq.com
 * @date 2022-09-05 09:44:00
 */
@RestController
@RequestMapping("score/score")
//@Api(tags = "文件上传")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private SysUserService sysUserService;
    @Value("${img}")
    private String adresses;
    @Autowired
    private ProveService proveService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("score:score:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scoreService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("score:score:info")
    public R info(@PathVariable("id") Integer id){
		ScoreEntity score = scoreService.getById(id);

        return R.ok().put("score", score);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("score:score:save")
    public R save(@RequestBody ScoreEntity score){
		scoreService.save(score);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("score:score:update")
    public R update(@RequestBody ScoreEntity score){
		scoreService.updateById(score);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("score:score:delete")
    public R delete(@RequestBody Integer[] ids){
		scoreService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 计算
     */
    @RequestMapping("/count/{id}")
    public R count(@RequestBody int[] countVo,@PathVariable("id") Integer id){
      // 先去数据库看看该用户是否已经答过题
       ScoreEntity scoreEntity1=scoreService.getscore(id);
       if(scoreEntity1!=null){
           return R.error("您已经做过此问卷，请不要重复提交");
       }
       // 没有就计算分数并保存
      SysUserEntity sysUserEntity= sysUserService.getById(id);
        ScoreEntity score=new ScoreEntity();
        score.setScore(scoreService.getCount(countVo));
        score.setResponse(sysUserEntity.getUsername());
        score.setUserid(id);
        scoreService.save(score);

        return R.ok();
    }
    /**
     * 计算
     */
    @RequestMapping("/count2/{id}")
    public R count2(@RequestBody double[] countVo,@PathVariable("id") Integer id){
        // 先去数据库看看该用户是否已经答过题
        ScoreEntity scoreEntity1=scoreService.getscore(id);
        if(scoreEntity1!=null){
            return R.error("您已经做过此问卷，请不要重复提交");
        }
        // 没有就计算分数并保存
        SysUserEntity sysUserEntity= sysUserService.getById(id);
        ScoreEntity score=new ScoreEntity();
         double sum=0;
         for(int i=0;i<countVo.length;i++){
             sum=countVo[i]+sum;
         }
         score.setScore(sum);
        System.out.println(sum);
        score.setResponse(sysUserEntity.getUsername());
        score.setUserid(id);
        scoreService.save(score);
        return R.ok();
    }
    /**
     * 上传证明文件
     */
    @RequestMapping("/imgupload")
//    @ApiOperation("pdf文件上传")
    public R imgupload(@RequestParam("pictures") MultipartFile[] pictures, @RequestParam Map<String, Object> data){
        //获取文件在服务器的储存位置
        // String path = request.getSession().getServletContext().getRealPath("/upload");
        String path = adresses;
        SysUserEntity sysUserEntity= sysUserService.getById(Integer.valueOf((String)data.get("userid")));
        File filePath = new File(path);
        if (!filePath.exists() && !filePath.isDirectory()) {
            System.out.println("目录不存在，创建目录:" + filePath);
            filePath.mkdirs();
        }

        for (MultipartFile picture : pictures) {
            String originalFileName = picture.getOriginalFilename();
            //在指定路径下创建一个文件
            File targetFile = new File(path, originalFileName);
            ProveEntity proveEntity=new ProveEntity();
            proveEntity.setQuestionId(Long.valueOf((String) data.get("questionId")));
            proveEntity.setImg(originalFileName);
            proveEntity.setNumber(Integer.valueOf((String)data.get("number")));
            proveEntity.setUser(Integer.valueOf((String)data.get("userid")));
            proveEntity.setUsername(sysUserEntity.getUsername());
            proveService.save(proveEntity);
            //将文件保存到服务器指定位置
            try {
                picture.transferTo(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error(500, originalFileName + "上传失败");
            }
        }
        return R.ok();
    }
}
